var global_prompt_id = null;

function singleOptimize(){
    var message = $("#msg_text").val();
    $.ajax({
        url:api_path+'msg/singleOptimize',
        data:{message:message},
        type:'post',
        dataType:'json',
        success:function(response){
          if (response.code == 2000) {
            global_prompt_id = response.data;
            $("#singleOptimizeBtn").hide();
            $("#loadingOptimizeBtn").show();

            cchUtil.message('生成中，请稍后');
            getPrompt();
          }else{
            cchUtil.message(response.msg,'danger');
          }
        }
    });
}

function getPrompt(){
    if (global_prompt_id == null) { 
        $("#singleOptimizeBtn").show();
        $("#loadingOptimizeBtn").hide();
        return false; 
    }
    $.ajax({
        url:api_path+'msg/getPrompt',
        data:{task_id:global_prompt_id},
        type:'post',
        async:false,
        dataType:'json',
        success:function(response){
          if (response.code == 2000) {
            global_prompt_id = null;
            $("#singleOptimizeBtn").show();
            $("#loadingOptimizeBtn").hide();

            var data = response.data;
            $("#prompt_content").html(data.content);
            $("#prompt_optimize").val(data.optimizeContent);

            $("#promptModal").modal("show");
            return false; 
          }else if(response.code == 5001){
            cchUtil.message('指令优化失败，请刷新重试','danger');

            global_prompt_id = null;
            $("#singleOptimizeBtn").show();
            $("#loadingOptimizeBtn").hide();
            return false; 
          }
        }
    });
    setTimeout(function() {
        getPrompt();
    }, 5000);
}

$(".prompt_paste").on('click',function(){
    $("#msg_text").val($("#prompt_optimize").val());
});